;(function() {

  new Vue({
    el: '#ssDetailsReport',
    data: {
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      data: [],
      selectedPaymentStateCode: [],
      selectedOverStateCode: [],
      khname: '',
      expenseId: [],
      expenseName: [],
      currentPage: 1,
      totalCount: 0,
      totalPage: 0,
      pageCount: ['10'],
      expenseList: [],
      isExtenseFlag:false,
			bill: {},
			projectList: [],
			buildingList: [],
			floorList: [],
			unitList: [],
			projectIDs: [],
			projectId: [],
			buildingId: [],
			floorId: [],
			unitId: [],
			showView: false,
      groupId:null,
      groupOneData:[],
      groupTwoData:[],
      groupThreeData:[],
      selectedGroupData:[],
      dataList:[],
      pageCounts: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      limit: ['10'],
      reportDate: null,
      beginDate: null,
      endDate: null,

    },
    filters: {
      formatDate: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatNum:function(item){
        if (item === undefined) return '--'
        return hlNumberUtil.formatNum(item)
      },
      formatUndefined: function(item){
        if (item === undefined) return '--'
        return item
      }
    },
    methods: {
      handleReportDate(data) {
        console.log(data);
        this.expenseId = []
        this.expenseName = []
        this.getExpenseList()
      },
      initTime() {
        this.reportDate = hlDateUtil.formatUTCDate(new Date(), 'yyyy-MM-dd')
      },
      //分页
      selectPerNum(arr) {
        this.getData()
      },
      expertExcel() {
        const self = this
        hlhttp.get({
          baseUrl: exclUrl,
          url: 'resource/owe_bill/_export',
          data: {
            "keyword": self.khname.trim() || '#',
            "expenditure_id": self.expenseId.length ? self.expenseId.join() : '#',
            'expenditure_name': self.expenseName.length ? self.expenseName.join() : '#',
            'payment_deadline_begin': self.beginDate || '#',
            'payment_deadline_end': self.endDate || '#',
            'report_date': self.reportDate || '#',
            'project_id': self.projectId.length ? self.projectId[0] : '#',
            'grouping_id':self.groupId || '#',
            'building_id': self.buildingId.length ? self.buildingId[0] : '#',
            'floor_id': self.floorId.length ? self.floorId[0] : '#',
            'unit_id': self.unitId.length ? self.unitId[0] : '#',
            "page": self.currentPage,
            "limit": self.limit[0]
          },
          success: (data) => {
            var a = document.createElement('a');
            a.href = data.excel_url;
            $('body').append($(a))
            a.click();
            setTimeout(function() {
              $(a).remove()
            }, 100);

          }
        })
      },
      handleSelectedExpense (arr){
        this.currentPage = 1
      },
      openSearch(){
        if(this.isExtenseFlag == true){
          this.isExtenseFlag = false;
					this.projectId = []
					this.buildingId = []
					this.floorId = []
					this.unitId = []
					this.buildingList = []
					this.floorList = []
					this.unitList = []
          return false;
        }

        if(this.isExtenseFlag == false){
          this.isExtenseFlag = true;
          return false;
        }
      },
      search(){
        this.currentPage = 1
        this.getData()
      },
      //获取列表 默认nums（当前页）: 1, cost（当前费项）: '#'
      getData() {
        var self = this, expenseId = JSON.parse(JSON.stringify(self.expenseId)) , expenseName = [];

        if(expenseId.indexOf('#') > -1) expenseId.splice(expenseId.indexOf('#'),1)

        self.expenseList.forEach(el => {
          if(expenseId.indexOf(el.code) > -1) expenseName.push(el.name)
        })

        var data = {
          "keyword": self.khname.trim() || '#',
          // "expenditure_id": expenseId.length ? expenseId.join() : '#',
          'expenditure_name': expenseName.length ? expenseName.join() : '#',
          'payment_deadline_begin': self.beginDate || '#',
          'payment_deadline_end': self.endDate || '#',
          'report_date': self.reportDate || '#',
          'project_id': self.projectId.length ? self.projectId[0] : '#',
          'grouping_id':self.groupId || '#',
          'building_id': self.buildingId.length ? self.buildingId[0] : '#',
          'floor_id': self.floorId.length ? self.floorId[0] : '#',
          'unit_id': self.unitId.length ? self.unitId[0] : '#',
          "page": self.currentPage,
          "limit": self.limit[0]
        }

        hlhttp.get({
          url: 'report/owe_bill',
          data: data,
          success: function(res) {
            self.totalPage = res.totalPage
            self.totalCount = res.totalCount
            self.dataList = res.records || []
          }
        })
      },
      //重置
      resetBtn() {
        this.khname = '';
        this.beginDate = null
        this.endDate = null
        this.expenseId = []
        this.expenseName = []
				this.projectId = []
        this.groupId = null
				this.buildingId = []
				this.floorId = []
				this.unitId = []
				this.buildingList = []
				this.floorList = []
				this.unitList = []
        this.selectedGroupData = []
        this.currentPage = 1

        this.initTime()
        this.getData();
      },
      //获取费项
      getExpenseList(item) {
        var self = this;
        hlhttp.get({
          url: 'report/owe_bill/expenses',
          data: { report_date: self.reportDate || '#' },
          success: function(res) {
						res.unshift({expenditureName: '全部', expenditureId: '#'})
            res.forEach(el => {
              el['code'] = el['expenditureId'] || new Date().getTime() + Math.random() * 1000
              el['name'] = el['expenditureName']
            })
            self.expenseList = res
            console.log(self.expenseList)
          }
        })
      },
			//获取项目
			getProjectData () {
        const self = this
        return new Promise(resolve => {
          hlhttp.get({
  			    url: 'user/projects',
  			    success: (res) => {
  						res = hlArrayUtil.removeRepeat(res, 'projectId')
  						self.projectList = hlArrayUtil.replaceData(res, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}])
  						self.projectList.map( item => self.projectIDs.push(item.projectId) )
              resolve(res)
  			    }
  			  })
        })
			},
      // 获取一级组合数据
      getGroupPrimaryData(data){
        const self = this
        self.project_id = [data[0].code]
        self.groupId = null
        self.buildingId = []
        self.floorId = []
        self.unitId = []
        self.buildingList = []
        self.floorList = []
        self.unitList = []
        self.selectedGroupData = []
        self.getGroupData().then(data => {
          self.groupOneData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] );
        })
        self.getBuildingData()
      },
      // 选取一级组合数据
      selectOneGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupTwoData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取二级组合数据
      selectTwoGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupThreeData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取三级组合数据
      selectThreeGroup(res){
        this.groupId = res['cur']['code']
      },
      getGroupData(){
        const self = this

        return new Promise(resolve => {
          hlhttp.get({
            url:'project/grouping_info/_subclass',
            data:{'project_id':self.project_id[0] , 'grouping_id':self.groupId || '#' },
            success:function(data){
              resolve(data)
            }
          })
        })
      },
			//获取楼宇
			getBuildingData () {
        const self = this
			  hlhttp.get({
			    url: 'project/grouping/_building',
			    data: { project_id: self.projectId.length ? self.projectId[0] : '#'  , grouping_id:self.groupId || '#'},
			    success: (data) => {
            data = data.filter(el => el.configState == 'Y')
			      this.buildingList = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
			    }
			  })
			},
			//获取楼层
			getfloorData () {
			  hlhttp.get({
			    url: 'building/floors',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0]},
			    success: (res) => {
			      this.floorList = hlArrayUtil.replaceData(res, [{key: 'floorName', rKey: 'name'}, {key: 'floorId', rKey: 'code'}]);
			    }
			  })
			},
			//获取单元
			getunitData () {
			  hlhttp.get({
			    url: 'floor/units',
			    data: {project_id: this.projectId[0], building_id: this.buildingId[0], floor_id: this.floorId[0]},
			    success: (res) => {
			      this.unitList = hlArrayUtil.replaceData(res, [{key: 'unitName', rKey: 'name'}, {key: 'unitId', rKey: 'code'}]);
			    }
			  })
			},
			//点击项目获得  id
			getprojectId (arr) {
			  this.projectId = [arr[0].code];
			  this.buildingId = [];
			  this.floorId = [];
			  this.unitId = [];
        this.getBuildingData()
			},
			//点击楼宇获得 id
			getbuildingId (arr) {
			  this.buildingId = [arr[0].code];
			  this.floorId = [];
			  this.unitId = [];
        this.getfloorData()
			},
			//点击楼层获得 id
			getfloorId (arr) {
			  this.floorId = [arr[0].code];
			  this.getunitData()
			  this.unitId = [];
			},
			// 点击单元获得 id
			getunitId (arr) {
			  this.unitId = [arr[0].code];
			},
    },
    mounted: function() {
      hlSelectUi(true)

      this.initTime()
      this.getProjectData()
      this.getExpenseList()
      this.getData()
    }
  })
}())
